package com.xiaoyu.awakening.user.controller;

import com.xiaoyu.awakening.common.core.dto.Result;
import com.xiaoyu.awakening.common.core.kit.ValidatorKit;
import com.xiaoyu.awakening.user.api.IUserLoginControllerApi;
import com.xiaoyu.awakening.user.dto.req.PasswdLoginReq;
import com.xiaoyu.awakening.user.dto.res.CaptchaRes;
import com.xiaoyu.awakening.user.dto.res.PasswdLoginRes;
import com.xiaoyu.awakening.user.enums.UserErrorCodeEnums;
import com.xiaoyu.awakening.user.service.IUserLoginService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * UserLoginApi
 *
 * @author wangxiaoyu
 * @since v1.0
 */
@Log4j2
@RestController
public class UserLoginController implements IUserLoginControllerApi {
    @Resource
    private IUserLoginService userLoginService;

    @Override
    public Result<PasswdLoginRes> passwdLogin(@RequestBody PasswdLoginReq passwdLoginReq) {
        String validMsg = ValidatorKit.validateEntityReturnMsg(passwdLoginReq);
        if (StringUtils.isNotEmpty(validMsg)) {
            return Result.fail();
        }
        PasswdLoginRes passwdLoginRes = userLoginService.passwdLogin(passwdLoginReq);
        Result<PasswdLoginRes> passwdLoginResResult = new Result<>();
        passwdLoginResResult.setSuccess(true);
        passwdLoginResResult.setData(passwdLoginRes);
        passwdLoginResResult.setResultMsg("用户登录成功！！！");
        return passwdLoginResResult;
    }

    @Override
    public Result<CaptchaRes> captcha() {
        CaptchaRes captcha = userLoginService.getCaptcha();
        return Result.success(captcha);
    }
}
